package com.example.homework.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.homework.R;
import com.example.homework.bean.Calculator;
import com.example.homework.bean.CalculatorData;
import com.example.homework.bean.CalculatorOperator;

public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener{
    private static final int[] btnIds={
            R.id.btn_ac,
            R.id.btn_percent,
            R.id.btn_add,
            R.id.btn_sub,
            R.id.btn_mu,
            R.id.btn_div,
            R.id.btn_one,
            R.id.btn_tow,
            R.id.btn_three,
            R.id.btn_four,
            R.id.btn_five,
            R.id.btn_six,
            R.id.btn_seven,
            R.id.btn_eight,
            R.id.btn_nine,
            R.id.btn_zero,
            R.id.btn_dot,
            R.id.btn_equals,


    };
    private Button[] btns;
    private TextView calContent;
    private Calculator calculator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        calculator = new Calculator();
        calContent = findViewById(R.id.result);
        Button btn;
        for(int btnId:btnIds){
            btn = findViewById(btnId);
            btn.setOnClickListener(this);
        }
    }
    @Override
    public void onClick(View view){
        String numberText = calContent.getText().toString();
        int id= view.getId();
        switch (id){
            case R.id.btn_ac:
                numberText=calculator.clickOperator(CalculatorOperator.CLEAR,numberText);
                break;
            case R.id.btn_percent:
                numberText=calculator.clickOperator(CalculatorOperator.PERCENT,numberText);
                break;
            case R.id.btn_add:
                numberText=calculator.clickOperator(CalculatorOperator.ADD,numberText);
                break;
            case R.id.btn_sub:
                numberText=calculator.clickOperator(CalculatorOperator.SUB,numberText);
                break;
            case R.id.btn_mu:
                numberText=calculator.clickOperator(CalculatorOperator.MU,numberText);
                break;
            case R.id.btn_div:
                numberText=calculator.clickOperator(CalculatorOperator.DIV,numberText);
                break;
            case R.id.btn_equals:
                numberText=calculator.clickOperator(CalculatorOperator.EQUALS,numberText);
                break;

            case R.id.btn_one:
                numberText=calculator.clickData(CalculatorData.ONE);
                break;
            case R.id.btn_tow:
                numberText=calculator.clickData(CalculatorData.TOW);
                break;
            case R.id.btn_three:
                numberText=calculator.clickData(CalculatorData.THREE);
                break;
            case R.id.btn_four:
                numberText=calculator.clickData(CalculatorData.FOUR);
                break;
            case R.id.btn_five:
                numberText=calculator.clickData(CalculatorData.FIVE);
                break;
            case R.id.btn_six:
                numberText=calculator.clickData(CalculatorData.SIX);
                break;
            case R.id.btn_seven:
                numberText=calculator.clickData(CalculatorData.SEVEN);
                break;
            case R.id.btn_eight:
                numberText=calculator.clickData(CalculatorData.EIGHT);
                break;
            case R.id.btn_nine:
                numberText=calculator.clickData(CalculatorData.NINE);
                break;
            case R.id.btn_zero:
                numberText=calculator.clickData(CalculatorData.ZERO);

        }
        calContent.setText(numberText);
    }
}
